package com.xszx.servlet;

import com.xszx.beans.Goods;
import com.xszx.dao.GoodsDao;
import com.xszx.dao.impl.GoodsDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddGoodsServlet")
public class AddGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改编码
        req.setCharacterEncoding("utf-8");

        //获取参数
        String goodsName = req.getParameter("goodsName");
        String goodsUnit = req.getParameter("goodsUnit");
        Double goodsPrice = Double.parseDouble(req.getParameter("goodsPrice"));
        String goodsImg = req.getParameter("goodsImg");
        String goodsInfo = req.getParameter("goodsInfo");
        String goodsAddress = req.getParameter("goodsAddress");
        int num = Integer.parseInt(req.getParameter("num"));
        int state = Integer.parseInt(req.getParameter("state"));

        GoodsDao goodsDao = new GoodsDaoImpl();
        Goods goods = new Goods(goodsName, goodsUnit, goodsPrice, goodsImg, goodsInfo, goodsAddress, num, state);
        goodsDao.addGoods(goods);

        resp.sendRedirect("GetGoodsServlet");
    }
}
